package com.qiyuexinxi.chat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiyuexinxi.chat.domain.ThumbInfo;
import com.qiyuexinxi.chat.dto.request.AddThumbInfoRequest;
import com.qiyuexinxi.chat.dto.request.GetUserInfoRequest;
import com.qiyuexinxi.chat.dto.request.ThumbInfoListRequest;
import com.qiyuexinxi.chat.dto.response.GetUserInfoResponse;
import com.qiyuexinxi.chat.dto.response.ThumbInfoListResponse;
import com.qiyuexinxi.chat.exception.reg.exception.BizErrorCodeEnum;
import com.qiyuexinxi.chat.exception.reg.exception.BizException;
import com.qiyuexinxi.chat.mapper.ThumbInfoMapper;
import com.qiyuexinxi.chat.service.ThumbInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangxingyu
 * @date 2020/3/24
 * @description
 */
@Service
public class ThumbInfoServiceImpl implements ThumbInfoService {

    @Autowired
    ThumbInfoMapper thumbInfoMapper;
    @Autowired
    UserServiceImpl userService;

    @Override
    public boolean addThumbInfo(AddThumbInfoRequest addThumbInfoRequest) {
        ThumbInfo thumbInfo = new ThumbInfo();
        BeanUtils.copyProperties(addThumbInfoRequest, thumbInfo);
        int i = thumbInfoMapper.insertSelective(thumbInfo);
        if (i <= 0) {
            throw new BizException(BizErrorCodeEnum.ADD_THUMBINFO_FAILED);
        }
        return true;
    }

    @Override
    public boolean deletedThumbInfo(AddThumbInfoRequest addThumbInfoRequest) {
        ThumbInfo thumbInfo = new ThumbInfo();
        BeanUtils.copyProperties(addThumbInfoRequest, thumbInfo);
        thumbInfo.setDeleted(0);
        int i = thumbInfoMapper.deletedThumbInfo(thumbInfo);
        if (i <= 0) {
            throw new BizException(BizErrorCodeEnum.DELETE_THUMBINFO_FAILED);
        }
        return true;
    }

    @Override
    public List<GetUserInfoResponse> getThumbInfoList(ThumbInfoListRequest thumbInfoListRequest) {
        //需要查用户信息的集合
        List<Integer> userIds = new ArrayList<Integer>();
        List<ThumbInfo> thumbInfos = thumbInfoMapper.selectByIdAndType(thumbInfoListRequest.getThumbAssociationId(), thumbInfoListRequest.getThumbAssociationType());
        for (ThumbInfo thumbInfo : thumbInfos) {
            Integer thumbUserId = thumbInfo.getThumbUserId();
            if (userIds.contains(thumbUserId)) {
                continue;
            }
            userIds.add(thumbUserId);
        }
        GetUserInfoRequest getUserInfoRequest = new GetUserInfoRequest(userIds);
        List<GetUserInfoResponse> userInfo = userService.getUserInfo(getUserInfoRequest);
        return userInfo;
    }

    @Override
    public Integer getThumbInfoCount(ThumbInfoListRequest thumbInfoListRequest) {
        return thumbInfoMapper.selectCountByIdAndType(thumbInfoListRequest.getThumbAssociationId(), thumbInfoListRequest.getThumbAssociationType());

    }

    /**
     * 获取当前用户点赞信息 （包括对当前内容是否点赞，当前内容点赞数量）
     * @param thumbAssociationId
     * @param thumbAssociationType
     * @param userId
     * @return
     */
    @Override
    public JSONObject getThumbInfo(Integer thumbAssociationId, Integer thumbAssociationType, Integer userId) {
        JSONObject result = new JSONObject();
        Integer thumbInfoCount = thumbInfoMapper.selectCountByIdAndType(thumbAssociationId, thumbAssociationType);
        result.put("thumbInfoCount",thumbInfoCount);
        Integer uesrCount = thumbInfoMapper.selectIsThumb(thumbAssociationId, thumbAssociationType, userId);
        if(uesrCount>0){
            result.put("userIsThumb",true);
        }else {
            result.put("userIsThumb",false);
        }
        return result;
    }


}
